<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddRepair" type="primary" style="float:right;margin-top:13px;">
      录入维修车辆
    </el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input placeholder="请输入" v-model="saveVehicleRepairForm.userName"
                      :disabled="true"></el-input>
            <!--            :value="user.username"
                        :disabled="true"-->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择" v-model="saveVehicleRepairForm.vehicleId">
              <el-option v-for="item in licenseOptions"
                         :label="item.license" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="saveVehicleRepairForm.item">
              <el-option v-for="item in itemTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="saveVehicleRepairForm.addr">
              <el-option v-for="item in addrTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="saveVehicleRepairForm.material">
              <el-option v-for="item in materialTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入" v-model="saveVehicleRepairForm.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleRepairForm.repairTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="saveVehicleRepairForm.status">
              <el-radio border label="维修中" value="1" style="margin: 0;"></el-radio>
              <el-radio border label="已完成" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicleRepair">录入</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;"
                  v-model="searchVehicleRepairForm.license" @keydown.enter="loadVehicleRepair"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" style="width:290px;">
        <el-input placeholder="请输入送修人姓名" style="width:220px;"
                  v-model="searchVehicleRepairForm.userName" @keydown.enter="loadVehicleRepair"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width: 290px;">
        <el-select placeholder="请选择维修状态" v-model="searchVehicleRepairForm.status" @change="loadVehicleRepair">
          <el-option label="已完成" value="0"></el-option>
          <el-option label="维修中" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicleRepair">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆维修列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleRepairArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="license" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center" :formatter="vehicleRepairItemFormatter"/>
      <el-table-column prop="userName" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center" :formatter="addrTypeArrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center" :formatter="materialTypeArrFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="vehicleRepairStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicleRepair(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicleRepair(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('录入维修车辆');
//定义对象用来保存用户登录信息
const user = ref(getUser());

//定义对象用来保存车辆搜索条件
const searchVehicleRepairForm = ref({license: '', userName: '', status: ''});
//定义数组用来保存车辆维修列表表格数据
const vehicleRepairArr = ref([]);

//定义加载车辆的方法
const loadVehicleRepair = () => {
  console.log(searchVehicleRepairForm.value);
  let data = qs.stringify(searchVehicleRepairForm.value);
  axios.get(BASE_URL + '/v1/repair/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      vehicleRepairArr.value = response.data.data;
      console.log(vehicleRepairArr.value)
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
//一进入页面立即加载车辆维修数据
onMounted(() => {
  loadVehicleRepair();
})
//定义重置搜索的方法
const resetSearch = () => {
  searchVehicleRepairForm.value = {};
  loadVehicleRepair();
}

//定义处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('您确认要关闭弹窗吗?')) {
    saveVehicleRepairForm.value = {userName: user.value.username, userId: user.value.id};
    dialogVisible.value = false;
  }
}

//对车辆维修状态显示内容进行文字转换
const vehicleRepairStatusFormatter = (row, column, cellValue, index) => {
  if (cellValue == 0) {
    cellValue = '已完成';
  } else if (cellValue == 1) {
    cellValue = '维修中';
  }
  return cellValue;
}

//对车辆维修状态显示内容进行文字转换
const vehicleRepairItemFormatter = (row, column, cellValue, index) => {
  if (cellValue == 10) {
    cellValue = '汽车保养';
  }
  if (cellValue == 20) {
    cellValue = '汽车美容';
  }
  if (cellValue == 30) {
    cellValue = '钣金喷漆';
  }
  if (cellValue == 40) {
    cellValue = '四轮定位';
  }
  if (cellValue == 50) {
    cellValue = '机修';
  }
  return cellValue;
}

//定义车辆表格(车辆类型列)显示文字转换的方法
const addrTypeArrFormatter = (row, column, cellValue, index) => {
  if (!addrTypeArr.value) {
    return cellValue;
  }
  for (let item of addrTypeArr.value) {
    if (item.value == cellValue) {
      return item.label;
    }
  }
  return cellValue;
}
//定义车辆表格(车辆类型列)显示文字转换的方法
const materialTypeArrFormatter = (row, column, cellValue, index) => {
  if (!materialTypeArr.value) {
    return cellValue;
  }
  for (let item of materialTypeArr.value) {
    if (item.value == cellValue) {
      return item.label;
    }
  }
  return cellValue;
}
//定义对象用来收集维修表单数据
const saveVehicleRepairForm = ref({
  userId: user.value.id,
  userName: user.value.username,
  license: '',
  vehicleId: '',
  item: '',
  addr: '',
  material: '',
  fee: '',
  repairTime: '',
  status: ''
});

//定义数组用来保存可选维修车辆数据
//const licenseOptions = ref([]);
const licenseOptions = ref([]);

//定义录入车辆维修弹窗的方法
const beforeAddRepair = () => {
  dialogVisible.value = true;
  loadVehicleLicense();
}

//定义加载维修车辆车牌号的方法
const loadVehicleLicense = () => {
  //let userName = user.value.username;
  //console.log("userName="+userName)
  axios.get(BASE_URL + "/v1/vehicle/select").then((response) => {
    if (response.data.code == 2000) {
      licenseOptions.value = response.data.data;
      console.log("前端收到的:" + licenseOptions.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义录入维修车辆的方法
const saveVehicleRepair = () => {
  let data = qs.stringify(saveVehicleRepairForm.value);
  console.log("录入的信息:" + data);
  axios.post(BASE_URL + "/v1/repair/save", data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success("保存成功");
      saveVehicleRepairForm.value = {userName: user.value.username, userId: user.value.id};
      dialogVisible.value = false;
      loadVehicleRepair();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义编辑维修车辆的方法
const editVehicleRepair = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑维修车辆';
  axios.get(BASE_URL + "/v1/repair/select?id=" + id).then((response) => {
    if (response.data.code == 2000) {
      saveVehicleRepairForm.value = response.data.data[0];
      console.log("回显的信息:" + saveVehicleRepairForm.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义删除车辆维修的方法
const deleteVehicleRepair = (id) => {
  if (confirm('您确认要删除此条记录吗?')) {
    axios.post(BASE_URL + "/v1/repair/delete/" + id).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success("删除成功");
        loadVehicleRepair();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

//定义数组储存车辆维修字典项
//const vehicleIdArr = ref([]);
const itemTypeArr = ref([]);
const addrTypeArr = ref([]);
const materialTypeArr = ref([]);
onMounted(() => {
  //loadDictOptions(vehicleIdArr,'vehicle_type');
  loadDictOptions(itemTypeArr, 'repair_item');
  loadDictOptions(addrTypeArr, 'repair_addr');
  loadDictOptions(materialTypeArr, 'repair_material');
})
</script>